package com.example.hospitalsystem.service.Impl;

import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hospitalsystem.mapper.AdminMapper;
import com.example.hospitalsystem.mapper.DesignMapper;
import com.example.hospitalsystem.mapper.UserMapper;
import com.example.hospitalsystem.pojo.dto.design.AddDesignDto;
import com.example.hospitalsystem.pojo.dto.design.DesignPageQueryDto;
import com.example.hospitalsystem.pojo.entity.Admin;
import com.example.hospitalsystem.pojo.entity.Design;
import com.example.hospitalsystem.pojo.entity.User;
import com.example.hospitalsystem.result.Result;
import com.example.hospitalsystem.service.DesignService;
import com.example.hospitalsystem.utils.AliOssUtil;
import com.example.hospitalsystem.utils.JwtHelper;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DesignServiceImpl extends ServiceImpl<DesignMapper, Design> implements DesignService {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private DesignMapper designMapper;

    @Autowired
    private AliOssUtil aliOssUtil;

    @Override
    public Result<Object> addDesign(AddDesignDto addDesignDto) {

        String token=request.getHeader("Authorization");
        Integer userId=jwtHelper.getUserId(token);

        LambdaQueryWrapper<Design> designLambdaQueryWrapper=new LambdaQueryWrapper<>();
        designLambdaQueryWrapper.eq(Design::getDesignId,addDesignDto.getDesignId());
        Design design=designMapper.selectOne(designLambdaQueryWrapper);

        if(design==null){
          throw new ServiceException("错了");
        }
        BeanUtils.copyProperties(addDesignDto,design);

        LambdaUpdateWrapper<Design> designLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        designLambdaUpdateWrapper.eq(Design::getDesignId,addDesignDto.getDesignId());

        boolean i=this.update(design,designLambdaUpdateWrapper);

        if(i==false){
            return Result.build("插入数据信息失败");
        }
        return Result.build(design,"插入数据信息成功");
    }

    @Override
    public Result<Object> deleteDesignById(Integer id) {
        LambdaQueryWrapper<Design> designLambdaQueryWrapper=new LambdaQueryWrapper<>();
        designLambdaQueryWrapper.eq(Design::getDesignId,id);
        baseMapper.delete(designLambdaQueryWrapper);
        return Result.build("删除设计信息成功");
    }

    @Override
    public Result<Object> getAllDesign(DesignPageQueryDto dto) {

        Page<Design> designPage=new Page<>(dto.getPageNum(),dto.getPageSize());

        if(StringUtils.isNotEmpty(dto.getContent())){
            LambdaQueryWrapper<Design> designLambdaQueryWrapper=new LambdaQueryWrapper<>();
            designLambdaQueryWrapper.like(Design::getContent,"%"+dto.getContent()+"%");
            IPage<Design> designIPage=designMapper.selectPage(designPage,designLambdaQueryWrapper);
            return Result.build(designIPage,200,"获取所有设计信息成功");
        }
        IPage<Design> designIPage=designMapper.selectPage(designPage,null);
        return Result.build(designIPage,200,"获取所有设计信息成功");
    }

    @Override
    public Result<Object> getTop5Design() {
        LambdaQueryWrapper<Design>designLambdaQueryWrapper=new LambdaQueryWrapper<>();
        designLambdaQueryWrapper.orderByDesc(Design::getPublishTime).last("LIMIT 5");
        List<Design> designList=designMapper.selectList(designLambdaQueryWrapper);
        return Result.build(designList,200,"获取前五条设计信息成功");
    }

    @Override
    public Result<Object> getTop10Design() {
        LambdaQueryWrapper<Design>designLambdaQueryWrapper=new LambdaQueryWrapper<>();
        designLambdaQueryWrapper.orderByDesc(Design::getPublishTime).last("LIMIT 10");
        List<Design> designList=designMapper.selectList(designLambdaQueryWrapper);
        return Result.build(designList,200,"获取前十条设计信息成功");
    }

    @Override
    public Result<Object> getDetailShow(Integer id) {
        LambdaQueryWrapper<Design> designLambdaQueryWrapper=new LambdaQueryWrapper<>();
        designLambdaQueryWrapper.eq(Design::getDesignId,id);
        Design design=baseMapper.selectOne(designLambdaQueryWrapper);
        if(design==null){
            throw new RuntimeException("没有该设计信息");
        }
        return Result.build(design,200,"获取相信设计信息成功");
    }

    @Override
    public Result<Object> uploadDesign(MultipartFile image) throws IOException {
        String url=aliOssUtil.upload(image);
        String token=request.getHeader("Authorization");
        Integer userId=jwtHelper.getUserId(token);
        LambdaQueryWrapper<Admin> adminLambdaQueryWrapper=new LambdaQueryWrapper<>();
        adminLambdaQueryWrapper.eq(Admin::getUserId,userId);
        Admin admin=adminMapper.selectOne(adminLambdaQueryWrapper);

        LambdaQueryWrapper<User> userLambdaQueryWrapper=new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getUserId,userId);
        User user=userMapper.selectOne(userLambdaQueryWrapper);

        Design design=new Design();
        design.setImage(url);
        design.setAdminName(admin.getAdminName());
        design.setUserName(user.getUsername());
        design.setPublishTime(LocalDateTime.now());
        baseMapper.insert(design);

        LambdaQueryWrapper<Design> designLambdaQueryWrapper=new LambdaQueryWrapper<>();
        designLambdaQueryWrapper.eq(Design::getImage,url);
        Design design1=designMapper.selectOne(designLambdaQueryWrapper);

        if(design==null){
            throw new RuntimeException("设计数据为空");
        }
        return Result.build(design1,200,"上传图片信息成功");
    }
}
